package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: ProductController
 * @Package: com.atguigu.spzx.manager.controller
 * @Description 商品管理
 * @Author 孤妤
 * @Date: 2023/12/8 16:44
 */
@Tag(name = "商品管理")
@RestController
@RequestMapping("/admin/product/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @GetMapping("/{pageNum}/{pageSize}")
    @Operation(summary = "商品管理分页")
    public Result<PageInfo<Product>> findProductPage(@PathVariable Integer pageNum,
                                                     @PathVariable Integer pageSize,
                                                     ProductDto productDto){
        PageInfo<Product> pageInfo = productService.findProductPage(pageNum,pageSize,productDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/save")
    @Operation(summary = "添加商品管理")
    public Result addProduct(@RequestBody Product product){
        productService.addProduct(product);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("添加成功");
    }
    @GetMapping("/getById/{id}")
    @Operation(summary = "指定id查询商品管理信息")
    public Result<Product> findProductById(@PathVariable Long id){
        Product product = productService.findProductById(id);
        return Result.build(product,ResultCodeEnum.SUCCESS);
    }
    @PutMapping("/updateById")
    @Operation(summary = "修改商品管理信息")
    public Result changeProductById(@RequestBody Product product){
        productService.changeProductById(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("/deleteById/{id}")
    @Operation(summary = "删除商品管理信息")
    public Result removeProductById(@PathVariable Long id){
        productService.removeProductById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/updateStatus/{id}/{status}")
    @Operation(summary = "商品下架与上架")
    public Result changeProductStatusById(@PathVariable Long id,
                                          @PathVariable Integer status){
        Product product = productService.changeProductStatusById(id, status);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg(product.getAuditMessage());
    }
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    @Operation(summary = "商品的审批")
    public Result changeProductAuditStatusById(@PathVariable Long id,
                                          @PathVariable Integer auditStatus){
        Product product = productService.changeProductAuditStatusById(id, auditStatus);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg(product.getAuditMessage());
    }
}
